package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseControllerApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.token.TokenResult;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.common.constant.ContentErrorCode;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseControllerApi {

    @Autowired
    private CourseBaseService courseBaseService;

    @Autowired
    private RestTemplate restTemplate;

    @Value("${file.service.url}")
    private String url;

    @Value("${file.service.bucket}")
    private String scope;

    @Value("${file.token.deadline}")
    private int deadLine;

    @Value("${file.token.type}")
    private String tokenType;

    @Value("${cdn.domain}")
    private String domain;

    @Value("${file.service.upload.region}")
    private String region;

    private String key;



    @Override
    @PostMapping("/courses/all")
    public PageVO<CourseBase> queryCourseBaseList() {
        PageVO<CourseBase> courseBases = courseBaseService.queryCourseBaseList();
        return courseBases;
    }

    @Override
    @PostMapping("/course/list")
    public PageVO queryCourseList(PageRequestParams params, @RequestBody QueryCourseModel model) {
        //获得令牌并获得解析后的companyId
        Long companyId = SecurityUtil.getCompanyId();
        PageVO<CourseBaseDTO> courseBases = courseBaseService.queryCourseList(params, model,companyId);
        return courseBases;
    }

    @Override
    @PostMapping("/course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        //将vo转换为dto
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        //获得机构ID并存到courseBaseDTO中
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseDTO.setCompanyId(companyId);
        CourseBaseDTO courseBase = courseBaseService.createCourseBase(courseBaseDTO);
        return courseBase;
    }

    @Override
    @GetMapping("/course/{courseBaseId}")
    public CourseBaseDTO getCourseById(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        CourseBaseDTO dto = courseBaseService.getCourseById(courseBaseId, companyId);
        return dto;
    }

    @Override
    @PutMapping("/course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseDTO.setCompanyId(companyId);
        CourseBaseDTO dto = courseBaseService.modifyCourseBase(courseBaseDTO);
        return dto;
    }

    @Override
    @DeleteMapping("/course/{courseBaseId}")
    public void removeCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseService.removeCourseBase(courseBaseId,companyId);
    }

    @Override
    @GetMapping("/common/qnUploadToken")
    public TokenResult getUpLoadToken() {
        //直接在实现层完成业务

        //配置远程调用需要的参数
        HashMap<String, Object> map = new HashMap<>();
        map.put("scope", scope);
        map.put("tokenType", tokenType);
        map.put("deadLine", deadLine);

        String key = UUID.randomUUID().toString()+ RandomStringUtils.randomAlphanumeric(16);
        map.put("key", key);

        //远程调用
        ResponseEntity<Map> response = restTemplate.postForEntity(url, map, Map.class);

        int statusCodeValue = response.getStatusCodeValue();

        if (!(HttpStatus.OK.value() == statusCodeValue)) {
            ExceptionCast.cast(ContentErrorCode.E_120025);
        }
        Map body = response.getBody();
        String token = (String) body.get("result");
        //将令牌信息封装到TokenResult类中

        TokenResult tokenResult = new TokenResult();
        tokenResult.setTokenType(tokenType);
        tokenResult.setDeadLine(deadLine);
        tokenResult.setDomain(domain);
        tokenResult.setUp_region(region);
        tokenResult.setScope(scope);
        tokenResult.setKey(key);
        tokenResult.setQnToken(token);

        return tokenResult;
    }

    @Override
    @GetMapping("/course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseService.commitCourseBase(courseBaseId,companyId);
    }


}
